home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 051-075 / scopedisk75 / ispell / src / tree.c < prev   
C/C++ Source or Header  |  1995-03-19  |  3KB  |  193 lines

  1. /* -*- Mode:Text -*- */
  2. /*
  3.  * tree.c - a tree style dictionary for user's personal words
  4.  *
  5.  * Pace Willisson, 1983
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include "ispell.h"
  11.  
  12. char *getenv();
  13. char *upcase();
  14.  
  15. static struct node *root = NULL;
  16. struct node *tinsert();
  17.  
  18. static char personaldict[100];
  19. static FILE *dictf;
  20. static newwords = 0;
  21.  
  22. treeinit ()
  23. {
  24.     char *p;
  25.     char buf[BUFSIZ];
  26.  
  27.     p = getenv ("HOME");
  28.     if (p == NULL)
  29. #ifndef AMIGA
  30.         return;
  31. #else
  32.     p = LIBDIR;
  33. #endif
  34.  
  35.     strcpy (personaldict, p);
  36.  
  37. #if USEDEVICES
  38.     strcat (personaldict, "ispell.words");
  39. #else
  40.     strcat (personaldict, "/ispell.words");
  41. #endif
  42.  
  43.     if ((dictf = fopen (personaldict, "r")) == NULL)
  44.         return;
  45.  
  46.     while (fgets (buf, sizeof buf, dictf) != NULL) {
  47.         int len = strlen (buf) - 1;
  48.  
  49.         if (buf [ len ] == '\n')
  50.             buf [ len ] = 0;
  51.         treeinsert (buf, 1);
  52.     }
  53.  
  54.     fclose (dictf);
  55.  
  56.     newwords = 0;
  57.  
  58.     if (!lflag && !aflag && access (personaldict, 2) < 0)
  59.         printf ("Warning: Cannot update personal dictionary (%s)\r\n", personaldict);
  60. }
  61.  
  62. treeprint ()
  63. {
  64.     printf ("(");
  65.     tprint (root);
  66.     printf (")");
  67. }
  68.  
  69. static
  70. tprint (root)
  71. struct node *root;
  72. {
  73.     if (root == NULL)
  74.         return;
  75.     printf ("%s ", root->word);
  76.     tprint (root->left);
  77.     tprint (root->right);
  78. }
  79.  
  80.  
  81. treeinsert (word, keep)
  82. char *word;
  83. {
  84.     char nword[BUFSIZ];
  85.     strcpy (nword, word);
  86.     root = tinsert (upcase (nword), root, keep);
  87.     newwords = 1;
  88. }
  89.  
  90. static
  91. struct node *
  92. tinsert (word, root, keep)
  93. char *word;
  94. struct node *root;
  95. {
  96.     int cmp;
  97.  
  98.     if (root == NULL) {
  99.         root = (struct node *) calloc (1, sizeof (struct node));
  100.         root->word = (char *) malloc (strlen (word) + 1);
  101.         strcpy (root->word, word);
  102.         root->keep = keep;
  103.         return (root);
  104.     }
  105.  
  106.     cmp = strcmp (word, root->word);
  107.  
  108.     if (cmp == 0)
  109.         return (root);
  110.  
  111.     if (cmp < 0)
  112.         root->left = tinsert (word, root->left, keep);
  113.     else
  114.         root->right = tinsert (word, root->right, keep);
  115.  
  116.     return (root);
  117. }
  118.  
  119. treelookup (word)
  120. char *word;
  121. {
  122.     char nword[BUFSIZ];
  123.     strcpy (nword, word);
  124.     if (tlookup (upcase (nword), root)) {
  125.         return (1);
  126.     }
  127.     return (0);
  128. }
  129.  
  130. static
  131. tlookup (word, root)
  132. char *word;
  133. struct node *root;
  134. {
  135.     int cmp;
  136.  
  137.     if (root == NULL)
  138.         return (0);
  139.  
  140.     cmp = strcmp (word, root->word);
  141.  
  142.     if (cmp == 0)
  143.         return (1);
  144.  
  145.     if (cmp < 0)
  146.         return (tlookup (word, root->left));
  147.     else
  148.         return (tlookup (word, root->right));
  149. }
  150.  
  151. treeoutput ()
  152. {
  153.     if (newwords == 0)
  154.         return;
  155.  
  156.     if ((dictf = fopen (personaldict, "w")) == NULL) {
  157.         fprintf (stderr, "Can't create %s\r\n", personaldict);
  158.         return;
  159.     }
  160.  
  161.     toutput1 (root);
  162.  
  163.     fclose (dictf);
  164. }
  165.  
  166. static
  167. toutput1 (root)
  168. struct node *root;
  169. {
  170.     if (root == NULL)
  171.         return;
  172.  
  173.     if (root->keep)
  174.         fprintf (dictf, "%s\n", root->word);
  175.  
  176.     toutput1 (root->left);
  177.     toutput1 (root->right);
  178. }
  179.  
  180. char *
  181. upcase (s)
  182. register char *s;
  183. {
  184.     register char *os = s;
  185.  
  186.     while (*s) {
  187.         if (islower (*s))
  188.             *s = toupper (*s);
  189.         s++;
  190.     }
  191.     return (os);
  192. }
  193.